[SECTION .s16]
[BITS 16]

;/*
;  Flat memory module

;  kernel run in a flat memory space, base address is 0x0, segment size is 4G

;  0x00000000 - 0x00007BFF unused
;  0x00007C00 - 0x00007DFF boot sector
;  0x00007E00 - 0x00007FFF temp space for load GDTR
;  0x00008000 - 0x000080FF GDT table(temp space for boot sector)
;  0x00008100 - 0x00008BFF unused
;  0x00008C00 - 0x00009DFF FAT space for boot sector
;  0x00009E00 - 0x0000FFFF unused
;  0x00010000 - 0x0001FFFF run time space for bootloader
;  0x00020000 - 0x0009FFFF kernel file space with text, data, bss, rdata
;  0x000A0000 - 0x000FFFFF BIOS space
;  0x00100000 - 0x001007FF interrupt table
;  0x00100800 - 0x00100FFF unused
;  0x00101000 - 0x001FFFFF kernel stack
;  0x00200000 - 0xFFFFFFFF unused

;  global segment:
;  index     type    base        limit       used for
;  0x0008    data    0x00000000  0xFFFFFFFF  ds, es, fs, gs
;  0x0010    code    0x00000000  0xFFFFFFFF  cs
;  0x0018    stack   0x00000000  0xFFFFFFFF  ss
; */

global _Goto32
_Goto32:

  xor ax, ax
  mov ds, ax

  ;store GDT ptr

  mov si, 7FE2h
  lgdt [ds:si]

  ;enable A20
  cli
  in al, 92h
  or al, 00000010b
  out 92h, al

  ;to protect mode
  mov eax, cr0
  or eax, 1
  mov cr0, eax

  ;init regs
  mov ax, 0008h
  mov ds, ax
  mov es, ax
  mov fs, ax
  mov gs, ax
  mov ax, 0018h
  mov ss, ax

  xor eax, eax
  xor ebx, ebx
  xor ecx, ecx
  xor edx, edx

  xor edi, edi
  xor esi, esi
  mov esp, 001FFFFFh
  mov ebp, 001FFFFFh

  ;go to 32-bit code now !
  jmp dword 0x0010:00020000h

global _get_mem_info
_get_mem_info:

  push es

  mov ax, 0900h
  mov es, ax

  mov si, 0
  mov ebx, 0
  mov edi, 20h

loop:
  mov eax, 0E820h
  mov ecx, 0800h
  sub ecx, edi
  jna memcheckerror
  mov edx, 0534D4150h

  int 15h
  jc memcheckerror

  add di, cx
  cmp ebx, 0
  je memcheckok

  inc si
  cmp di, 800h
  jna loop

memcheckerror:
  mov si, 0
memcheckok:
  mov di, 0
  mov [es:di], si
  mov di, 4
  mov [es:di], cx
  pop es

  retf